package com.zzptc.ygz;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.*;
import java.net.Socket;

public class Clientl {
    private DefaultListModel dt;
    private JTextArea jta;
    private JTextField jtf_message = new JTextField(30);
    private JTextField jtf_port = new JTextField("9999");
    private JTextField jtf_max = new JTextField("30");
    private JButton jbt_start = new JButton("连接");
    private JButton jbt_stop = new JButton("断开");
    private String str;
    private Socket socket  ;
    public void createUI(){
        JFrame jf = new JFrame("客户端");
        jf.setBounds(200,200,500,300);
        //北边Jpanel
        JPanel northPane = new JPanel();
        northPane.setLayout(new GridLayout(1,6));
        northPane.add(new JLabel("人数上限"));
        northPane.add(jtf_max);
        northPane.add(new JLabel("端口"));
        northPane.add(jtf_port);
        northPane.add(jbt_start);
        jbt_stop.setEnabled(false);
        northPane.add(jbt_stop);
        northPane.setBorder(new TitledBorder("配置信息"));
        //中间Jpanl(左右分栏)
        //左栏
        dt = new DefaultListModel();
        JList jl = new JList(dt);
        JScrollPane leftpane = new JScrollPane(jl);
        leftpane.setBorder(new TitledBorder("在线用户"));
        //右栏
        jta = new JTextArea();
        jta.setEnabled(false);
        JScrollPane reghtpane = new JScrollPane(jta);
        reghtpane.setBorder(new TitledBorder("消息显示区"));
        JSplitPane centPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,leftpane,reghtpane);
        //设置分割条大小位置
        centPane.setDividerSize(20);
        centPane.setDividerLocation(100);
        //南部Jpanel
        JPanel southPanel = new JPanel();
        southPanel.setBorder(new TitledBorder("写消息"));
        southPanel.add(jtf_message,BorderLayout.CENTER );
        jf.setLayout(new BorderLayout());
        jf.add(northPane,BorderLayout.NORTH);
        jf.add(centPane,BorderLayout.CENTER);
        jf.add(southPanel,BorderLayout.SOUTH);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
        //服务器界面
        myEevent();
    }

    private void myEevent() {
        jtf_message.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyChar()==KeyEvent.VK_ENTER){
                    try {
                        OutputStream out = socket.getOutputStream();
                        PrintWriter pw = new PrintWriter(out);
                        pw.println(jtf_message.getText());
                        jta.append(jtf_message.getText()+"\n");
                        jtf_message.setText("");
                        pw.flush();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        });


        jbt_start.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    socket = new Socket("127.0.0.1", 9999);
                    InputStream in = socket.getInputStream();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            while (true) {
                                try {
                                    str = bufferedReader.readLine();
                                    jta.append(str +"\n");
                                } catch (IOException e1) {
                                    e1.printStackTrace();
                                }

                            }
                        }
                    }).start();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        });
    }



    public static void main(String[] args) {
    new Clientl().createUI();
    }
}
